package com.huihong.internship.system.Service.ServiceImpl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.huihong.internship.system.DTO.InternshipMaterialDTO;
import com.huihong.internship.system.DTO.InternshipMaterialPageQueryDTO;
import com.huihong.internship.system.DTO.PositionPageQueryDTO;
import com.huihong.internship.system.Mapper.StudentMapper;
import com.huihong.internship.system.Service.StudentService;
import com.huihong.internship.system.VO.EnterPriseVO;
import com.huihong.internship.system.VO.InternshipMaterialItemVO;
import com.huihong.internship.system.VO.PositionVO;
import com.huihong.internship.system.entity.InternshipScore;
import com.huihong.internship.system.entity.SysStudent;
import com.huihong.internship.system.result.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl  implements StudentService {

    @Autowired
    private StudentMapper studentMapper;



    /**
     * 获取学生信息
     * @return
     */
    @Override
    public SysStudent getInfo(Long userId) {
        SysStudent student = studentMapper.getInfo(userId);
        return student;
    }


    /**
     * 修改学生信息
     */
    @Override
    public void updateStu(SysStudent sysStudent) {
        studentMapper.updateInfo(sysStudent);

    }


    /**
     * 岗位分页查询
     * @param positionPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(PositionPageQueryDTO positionPageQueryDTO) {
        Integer pageNumber = positionPageQueryDTO.getPage();
        Integer pageSize = positionPageQueryDTO.getPageSize();
        if (pageNumber == null || pageNumber <= 0) {
            pageNumber = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 10;
        }

        PageHelper.startPage(pageNumber, pageSize);
        Page<PositionVO> page = studentMapper.getPageInfo(positionPageQueryDTO);
        long total = page.getTotal();
        List<PositionVO> result = page.getResult();

        return new PageResult(total,result);
    }


    /**
     * 根据ID查询企业
     * @param enterpriseId
     * @return
     */
    @Override
    public EnterPriseVO getEnterPriseById(Long enterpriseId) {
        EnterPriseVO enterPriseVO = studentMapper.getEnterPriseById(enterpriseId);
        return enterPriseVO;
    }

    @Override
    public Long submitMaterial(Long userId, InternshipMaterialDTO dto) {
        // 检查学生记录是否存在
        if (userId == null) {
            throw new RuntimeException("用户ID不能为空");
        }
        
        // 设置默认值
        if (dto.getSubmitTime() == null) {
            dto.setSubmitTime(new java.util.Date());
        }
        
        try {
            studentMapper.insertMaterial(userId, dto);
            return dto.getMaterialId();
        } catch (Exception e) {
            throw new RuntimeException("材料提交失败：" + e.getMessage());
        }
    }


    /**
     * 分页查询材料申请
     * @param userId
     * @param dto
     * @return
     */
    @Override
    public PageResult materialsPageQuery(Long userId, InternshipMaterialPageQueryDTO dto) {
        Integer pageNum = dto.getPageNum() == null || dto.getPageNum() <= 0 ? 1 : dto.getPageNum();
        Integer pageSize = dto.getPageSize() == null || dto.getPageSize() <= 0 ? 10 : dto.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        
        Page<InternshipMaterialItemVO> page = studentMapper.materialsPageQuery(userId, dto);
        return new PageResult(page.getTotal(), page.getResult());
    }


    /**
     * 查看成绩与反馈
     * @param userId
     * @return
     */
    @Override
    public InternshipScore getEvaluation(Long userId) {
        return studentMapper.getEvaluation(userId);
    }


}
